package com.liao.search.controller;

import com.liao.common.doc.ProductDoc;
import com.liao.search.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

/**
 * @Author: ljt
 * @Description: es保存
 * @DateTime: 2025/3/12 下午8:16
 **/
@Slf4j
@RequestMapping("/search/save")
@RestController
public class SaveController {

    @Autowired
    private ProductService productService;

    /**
     * 上架商品
     */
    @PostMapping("product")
    public void productStatusUp(@RequestBody List<ProductDoc> ProductDocList) {
        boolean result = false;// 是否执行成功
        try {
            result = productService.productStatusUp(ProductDocList);
        } catch (IOException e) {
            // es客户端连接失败
            log.error("ElasticSaveController商品上架错误：{}", e);
//            return R.error(500, "Elasticsearch客户端连接失败");
            throw new RuntimeException("Elasticsearch客户端连接失败");
        }
        if (result) {
//            return R.ok();// 执行成功
        } else {
//            return R.error(500, "Elasticsearch商品上架失败");
            throw new RuntimeException("Elasticsearch商品上架失败");
        }
    }

}
